package cn.stylefeng.guns.modular.miniapp.controller;

import org.slf4j.*;
import org.apache.commons.lang3.*;
import cn.stylefeng.guns.modular.miniapp.config.*;
import cn.binarywang.wx.miniapp.api.*;
import java.util.*;
import cn.binarywang.wx.miniapp.bean.*;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping({ "/wx/portal/{appid}" })
public class WxPortalController
{
    private final Logger logger;
    
    public WxPortalController() {
        this.logger = LoggerFactory.getLogger((Class)this.getClass());
    }
    
    @GetMapping(produces = { "text/plain;charset=utf-8" })
    public String authGet(@PathVariable final String appid, @RequestParam(name = "signature", required = false) final String signature, @RequestParam(name = "timestamp", required = false) final String timestamp, @RequestParam(name = "nonce", required = false) final String nonce, @RequestParam(name = "echostr", required = false) final String echostr) {
        this.logger.info("\n\u63a5\u6536\u5230\u6765\u81ea\u5fae\u4fe1\u670d\u52a1\u5668\u7684\u8ba4\u8bc1\u6d88\u606f\uff1asignature = [{}], timestamp = [{}], nonce = [{}], echostr = [{}]", new Object[] { signature, timestamp, nonce, echostr });
        if (StringUtils.isAnyBlank(new CharSequence[] { signature, timestamp, nonce, echostr })) {
            throw new IllegalArgumentException("\u8bf7\u6c42\u53c2\u6570\u975e\u6cd5\uff0c\u8bf7\u6838\u5b9e!");
        }
        final WxMaService wxService = WxMaConfiguration.getMaService(appid);
        if (wxService.checkSignature(timestamp, nonce, signature)) {
            return echostr;
        }
        return "\u975e\u6cd5\u8bf7\u6c42";
    }
    
    @PostMapping(produces = { "application/xml; charset=UTF-8" })
    public String post(@PathVariable final String appid, @RequestBody final String requestBody, @RequestParam(name = "msg_signature", required = false) final String msgSignature, @RequestParam(name = "encrypt_type", required = false) final String encryptType, @RequestParam(name = "signature", required = false) final String signature, @RequestParam("timestamp") final String timestamp, @RequestParam("nonce") final String nonce) {
        this.logger.info("\n\u63a5\u6536\u5fae\u4fe1\u8bf7\u6c42\uff1a[msg_signature=[{}], encrypt_type=[{}], signature=[{}], timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ", new Object[] { msgSignature, encryptType, signature, timestamp, nonce, requestBody });
        final WxMaService wxService = WxMaConfiguration.getMaService(appid);
        final boolean isJson = Objects.equals(wxService.getWxMaConfig().getMsgDataFormat(), "JSON");
        if (StringUtils.isBlank((CharSequence)encryptType)) {
            WxMaMessage inMessage;
            if (isJson) {
                inMessage = WxMaMessage.fromJson(requestBody);
            }
            else {
                inMessage = WxMaMessage.fromXml(requestBody);
            }
            this.route(inMessage, appid);
            return "success";
        }
        if ("aes".equals(encryptType)) {
            WxMaMessage inMessage;
            if (isJson) {
                inMessage = WxMaMessage.fromEncryptedJson(requestBody, wxService.getWxMaConfig());
            }
            else {
                inMessage = WxMaMessage.fromEncryptedXml(requestBody, wxService.getWxMaConfig(), timestamp, nonce, msgSignature);
            }
            this.route(inMessage, appid);
            return "success";
        }
        throw new RuntimeException("\u4e0d\u53ef\u8bc6\u522b\u7684\u52a0\u5bc6\u7c7b\u578b\uff1a" + encryptType);
    }
    
    private void route(final WxMaMessage message, final String appid) {
        try {
            WxMaConfiguration.getRouter(appid).route(message);
        }
        catch (Exception e) {
            this.logger.error(e.getMessage(), (Throwable)e);
        }
    }
}
